package com.jcbase.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.jcbase.core.auth.anno.RequiresPermissions;
import com.jcbase.core.controller.JCBaseController;
import com.jcbase.core.util.IWebUtils;
import com.jcbase.core.util.StringUtils;
import com.jcbase.core.view.InvokeResult;
import com.jcbase.model.SysObj;
import com.jcbase.model.SysRole;
import com.jcbase.model.SysSite;
import com.jcbase.model.SysUser;
import com.jcbase.model.SysUserRole;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 站点管理
 * 
 * @author Administrator
 *
 */

public class SiteController extends JCBaseController
{

	@RequiresPermissions(value = { "/sys/site" })
	public void index()
	{
		SysUser sysUser = IWebUtils.getCurrentSysUser(getRequest());
		List<SysUserRole> sysUserRole = SysUserRole.me
				.find("select * from sys_user_role where user_id = " + sysUser.getId());
		String roleId = "";
		for (SysUserRole sysUserRole2 : sysUserRole)
		{
			roleId = roleId + sysUserRole2.getRoleId();
		}
		this.setAttr("roleId", roleId);
		render("site_index.jsp");
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void siteUserSetView()
	{

		String id = this.getPara("id");
		if (id != null)
		{
			this.setAttr("id", id);
		}
		render("site_user_set.jsp");
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void setSiteView()
	{
		String oid = this.getPara("oid");

		if (oid != null)
		{
			this.setAttr("oid", oid);
		}
		render("obj_site_setting.jsp");
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void SiteUserSet()
	{
		String id = this.getPara("id");
		String siteId = this.getPara("siteId");
		List<SysSite> list = SysSite.me
				.find("select * from  sys_site where pid = 0 and id in(select DISTINCT(pid)  from sys_site where id in ("
						+ siteId + "))");
		if (CollectionUtils.isNotEmpty(list))
		{
			siteId = siteId + "," + list.get(0).getId();
		}
		if (!StringUtils.isBlank(id))
		{
			SysUser sysUser = SysUser.me.findById(id);
			if (sysUser != null)
			{
				sysUser.setSiteId(siteId);
				sysUser.update();
			}
			else
			{
				this.renderJson(InvokeResult.failure("操作失败，对象不存在"));
			}
		}
		this.renderJson(InvokeResult.success());
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void setSite()
	{
		String oid = this.getPara("oid");
		String siteId = this.getPara("siteId");
		List<Integer> idList = StringUtils.strGetList(oid);
		for (Integer integer : idList)
		{
			if (!StringUtils.isBlank(siteId))
			{
				// 查找对象
				SysObj sysObj = SysObj.me.findById(integer);
				if (sysObj != null)
				{
					sysObj.setSiteId(Integer.parseInt(siteId));
					sysObj.update();
				}
				else
				{
					this.renderJson(InvokeResult.failure("操作失败，对象不存在"));
				}

			}
		}
		this.renderJson(InvokeResult.success());
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void add()
	{
		Integer pid = this.getParaToInt("pid");
		Integer gid = this.getParaToInt("gid");
		if (gid != null)
		{
			this.setAttr("item", SysSite.me.findById(gid));
		}
		this.setAttr("gid", gid);
		this.setAttr("pid", pid);
		render("site_add.jsp");
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void save()
	{
		Integer pid = this.getParaToInt("pid");
		Integer gid = this.getParaToInt("gid");
		InvokeResult result;
		// 添加分组
		if (gid == null)
		{
			result = SysSite.me.addSave(pid, this.getPara("name"));
		}
		else
		{
			result = SysSite.me.updateSave(gid, this.getPara("name"));
		}
		this.renderJson(result);
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void delete()
	{
		String id = this.getPara("ids");
		// 判断该节点下是否还有子节点
		List<Record> listSite = Db.find("select * from sys_site where pid = " + id);
		if (CollectionUtils.isNotEmpty(listSite))
		{
			this.renderJson(InvokeResult.failure("该目录下还有目录，不能删除"));
			return;
		}
		else
		{// 判断该节点下是否还有对象
			List<Record> list = Db.find("select * from sys_obj where site_id = " + id);
			if (CollectionUtils.isNotEmpty(list))
			{
				this.renderJson(InvokeResult.failure("该目录下还有对象，不能删除"));
				return;
			}
			else
			{
				InvokeResult result = SysSite.me.delete(id);
				this.renderJson(result);
				return;
			}
		}
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void getAllList()
	{
		SysUser user = IWebUtils.getCurrentSysUser(getRequest());
		List<SysUserRole> list = SysRole.me.getRole(getRequest());
		List<SysSite> SiteList = new ArrayList<SysSite>();
		if (SysRole.me.checkIsAdmin(list))
		{
			SiteList = SysSite.me.getAllList();
		}
		else
		{
			if (!StringUtils.isBlank(user.getSiteId()))
			{
				SiteList = SysSite.me.find("select * from sys_site where id in (" + user.getSiteId() + ")");
			}

			// 遍历子节点
		}
		List<HashMap<String, Object>> SiteInfoList = new ArrayList<HashMap<String, Object>>();
		for (SysSite sysSite : SiteList)
		{
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", sysSite.getId());
			map.put("name", sysSite.getName());
			map.put("parent_id", sysSite.getPid());
			map.put("dirType", sysSite.getType());
			if ("未分配目录".equals(sysSite.getName()))
			{
				map.put("url", "/sys/obj/getNoGiveListView?siteId=" + sysSite.getPid());
			}
			else
			{
				map.put("url", "/sys/obj/getPublicObjView?siteId=" + sysSite.getId());
			}

			SiteInfoList.add(map);
		}

		this.renderJson(SiteInfoList);
	}

	public void addPrivateDir(String name, int pid, List<HashMap<String, Object>> SiteInfoList, int id, String url)
	{
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("parent_id", pid);
		map.put("type", 4);
		map.put("id", id);
		map.put("url", url);
		SiteInfoList.add(map);
	}

	@RequiresPermissions(value = { "/sys/site" })
	public void getPersonMenu()
	{
		int id = 1;
		SysUser sysUser = IWebUtils.getCurrentSysUser(getRequest());
		List<SysUserRole> list = SysRole.me.getRole(getRequest());
		List<SysSite> siteList = new ArrayList<SysSite>();
		if (SysRole.me.checkIsAdmin(list))
		{
			siteList = SysSite.me.find("select * from sys_site where  pid=0");
		}
		else
		{
			// 获取用户所有用站点
			if (!StringUtils.isBlank(sysUser.getSiteId()))
			{
				siteList = SysSite.me
						.find("select * from sys_site where id in (" + sysUser.getSiteId() + ") and pid=0");
			}
		}

		List<HashMap<String, Object>> SiteInfoList = new ArrayList<HashMap<String, Object>>();
		for (SysSite sysSite : siteList)
		{
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", sysSite.getId());
			map.put("name", sysSite.getName());
			map.put("parent_id", sysSite.getPid());
			map.put("dirType", sysSite.getType());
			SiteInfoList.add(map);
			if (id == sysSite.getId())
			{
				id = id + 1;
			}
			addPrivateDir("我的报表", sysSite.getId(), SiteInfoList, id++, "/sys/obj");
			addPrivateDir("我的收藏", sysSite.getId(), SiteInfoList, id++, "/sys/objColletc");
			addPrivateDir("我分享给别人的", sysSite.getId(), SiteInfoList, id++, "/sys/objShare/getShareObjOtherView");
			addPrivateDir("别人分享给我的", sysSite.getId(), SiteInfoList, id++, "/sys/objShare/getShareObjMeView");
		}

		this.renderJson(InvokeResult.success(SiteInfoList));

	}
}
